package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler.MultipleChargeAttrLinkHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Classname MultipleChargeAttrHandler
 * @Description 是否多方收费
 * @Author zhang.song
 * @Date 2022-01-30 15:42
 */
@Service
public class MultipleChargeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        this.setMultipleChargeAttr(attr, inst, data.getSceneInstId(), null);
        super.after(attr, inst, data);
    }

    // 订购、核查，非跨域，是否多方收费默认值否，不可修改
    public void setMultipleChargeAttr(KiteAttr attr, KiteInst inst, String sceneInstId, String orderNature) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        // 订购、核查 操作
        if (KiteStringUtils.equalsAny(kiteSceneInst.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_ORDER, LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)) {
            // 非跨域时，默认值=否，不可修改
            KiteProdInst prodInst = (KiteProdInst) inst;
            String catagoryId = Optional.ofNullable(ProductCache.get(prodInst.getProdId())).map(Product::getCateId).orElse(null);
            if(KiteStringUtils.equalsAny(catagoryId, ProdCateUtil.getDoubleLineCateIdForArr())) {
                // 订单性质=补录单（2），收费多方收费=否，不可修改
                if(KiteStringUtils.isEmpty(orderNature)) {
                    KiteAttr orderNatureAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.ORDER_NATURE);
                    orderNature = Optional.ofNullable(orderNatureAttr).map(KiteAttr::getValue).orElse(null);
                }
                if (LocalAttrUtils.isCrossDomain(kiteSceneInst, prodInst) && !KiteStringUtils.isEqual(orderNature, "2")) {
                    attr.setIsEdit(KeyConsts.IFTRUE_T);
                } else {
                    attr.setIsEdit(KeyConsts.IFTRUE_F);
                    attr.setValue("1_0000107");
                    attr.setValueDesc("否");
                }
                OperateInstAttrService operateInstAttrService = SpringUtil.getBean(OperateInstAttrService.class);
                operateInstAttrService.setAttrValue(inst, attr.getFieldName(), attr.getValue(), sceneInstId, null);

                AttrData attrData = new AttrData();
                KiteBeanUtils.copyProperties(kiteSceneInst, attrData);
                MultipleChargeAttrLinkHandler multipleChargeAttrLinkHandler = SpringUtil.getBean(MultipleChargeAttrLinkHandler.class);
                multipleChargeAttrLinkHandler.handle(attr, inst, attrData);
            }
        }
    }
}
